package com.hzdl.car.util;

import com.hzdl.car.bean.Customer;
import com.hzdl.car.bean.RentExcel;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class RentExcelUtil {


        /**
         * 使用客户信息数据，显示到Excel中,返回ByteArrayOutputStream
         */
        public static ByteArrayOutputStream  createExcelByRent(RentExcel rentExcel, String template ){


            try {
                InputStream is=RentExcel.class.getClassLoader().getResourceAsStream(template);
                HSSFWorkbook workbook=new HSSFWorkbook(is);
                //获取sheet对象
                HSSFSheet sheet=workbook.getSheetAt(0);
                int rownum=0;
                //第一行
                HSSFRow row1=sheet.getRow(rownum);
                HSSFCell row1_cell1=row1.getCell(0);
                row1_cell1.setCellValue(rentExcel.getCustName()+"的出租单");
                //第二行
                rownum++;
                HSSFRow row2=sheet.getRow(rownum);
                HSSFCell row2_cell2=row2.getCell(1);
                row2_cell2.setCellValue(rentExcel.getRentId());


                // 获取二维码
                InputStream logoStream=  RentExcelUtil.class.getClassLoader().getResourceAsStream("img03.png");
                System.out.println(logoStream);
                BufferedImage image =  ZXingUtil.createZXingCodeByLogo("出租单二维码",120,120,logoStream);

                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                // 把二维码转换为输出流对象
                ImageIO.write(image,"JPEG",bos);

                // 把二维码设置到excel中
                //画图的顶级管理器，一个sheet只能获取一个
                HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

                /**
                 * 参数4  设置图片的平铺程度  最大值是255  255代表铺满当前单元格 小于255就铺不满
                 * 参数5  列的开始坐标
                 * 参数6  行的开始坐标
                 * 参数7  列的结束坐标
                 * 参数8  行的结束坐标
                 */
                HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 255, (short) 4, 1, (short) 5, 1);
                anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
                patriarch.createPicture(anchor,workbook.addPicture(bos.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));

//                HSSFCell row2_cell5=row2.getCell(4);
//                row2_cell5.setCellValue("二维码");

                //第三行
                rownum++;
                HSSFRow row3=sheet.getRow(rownum);
                HSSFCell row3_cell2=row3.getCell(1);
                row3_cell2.setCellValue(rentExcel.getCustName());

                HSSFCell row3_cell5=row3.getCell(4);
                row3_cell5.setCellValue(rentExcel.getIdentity());

                //第四行
                rownum++;
                HSSFRow row4=sheet.getRow(rownum);
                HSSFCell row4_cell2=row4.getCell(1);
                row4_cell2.setCellValue(rentExcel.getBeginDate());

                HSSFCell row4_cell5=row4.getCell(4);
                row4_cell5.setCellValue(rentExcel.getReturnDate());

                //第五行
                rownum++;
                HSSFRow row5=sheet.getRow(rownum);
                HSSFCell row5_cell2=row5.getCell(1);
                row5_cell2.setCellValue(rentExcel.getCarNumber());

                HSSFCell row5_cell5=row5.getCell(4);
                row5_cell5.setCellValue("￥"+String.valueOf(rentExcel.getPrice()));

                //第六行
                rownum++;
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                HSSFRow row6=sheet.getRow(rownum);
                HSSFCell row6_cell5=row6.getCell(4);
                row6_cell5.setCellValue(sdf.format(new Date()));

                //第七行
                rownum++;
                HSSFRow row7=sheet.getRow(rownum);
                HSSFCell row7_cell5=row7.getCell(4);
                row7_cell5.setCellValue(rentExcel.getOpenName());



                ByteArrayOutputStream fos = new ByteArrayOutputStream();
                workbook.write(fos);

                return fos;
            } catch (Exception e) {
                e.printStackTrace();
            }

            return  null;



        }

}
